﻿using CourseUtils;
using StudentsPrenseLog.Extentions;
using StudentsPrenseLog.Models;
using StudentsPrenseLog.Repositories;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentsPrenseLog.Services
{
    public class StudentService
    {
        private StudentRepository _repository;

        public StudentService(StudentRepository repository)
        {
            if (repository == null)
                throw new ArgumentNullException("repository");

            _repository = repository;
        }

        public void DeleteStudents()
        {
            String searchString = IOHelpers.ReadStringFromConsole("Please enter search string");

            List<Student> students = _repository.FindStudents(searchString);

            if (students.Count == 0)
            {
                Console.WriteLine("No one student is found");
            }
            else
            {
                PrintStudents(students, String.Empty);

                int number = IOHelpers.ReadIntFromConsole("Please enter student number to delete");

                if (number - 1 < students.Count)
                {
                    _repository.Delete(students.ElementAt(number - 1));
                }
            }

        }

        private void PrintStudents(List<Student> students,String headerText)
        {
            Console.WriteLine(headerText);
            Console.WriteLine("****************************************************");

            int number = 1;

            foreach (Student std in students)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write(number++);
                Console.Write("#");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(std.ToString());
                Console.WriteLine("-----------------------------------------------------");
            }
        }

        public void FindStudents()
        {
            String searchString = IOHelpers.ReadStringFromConsole("Please enter search string");

            List<Student> students = _repository.FindStudents(searchString);

            PrintStudents(students, String.Format("There were {0} students found....", students.Count));
          
            Console.ReadKey();
        }

        public void CreateStudent()
        {
            String firstName  = IOHelpers.ReadStringFromConsole("Please enter student's first  name");
            String lastName   = IOHelpers.ReadStringFromConsole("Please enter student's last name");
            String format     = "dd/MM/yyyy";
            DateTime birthDay = IOHelperExtentions.ReadDateFromConsole(String.Format("Please enter student's birthdate in format({0})", format),format);
            float averageMark = IOHelperExtentions.ReadFloatFromConsole("Please enter student's average score");

            Student student = new Student(firstName,lastName,birthDay,averageMark);

            _repository.Create(student);
        }

        public void UpdateStudent()
        {
            String searchString = IOHelpers.ReadStringFromConsole("Please enter search string");

            List<Student> students = _repository.FindStudents(searchString);

            if (students.Count == 0)
            {
                Console.WriteLine("No one student is found");
            }
            else
            {
                PrintStudents(students, String.Empty);

                int number = IOHelpers.ReadIntFromConsole("Please enter student number to update");

                if (number - 1 < students.Count)
                {
                    string newFirstName = IOHelpers.ReadStringFromConsole("Please enter new student's name");
                    string newLastName  = IOHelpers.ReadStringFromConsole("Please enter new student's last name");

                    Student std = students.ElementAt(number - 1);

                    std.Rename(newFirstName, newLastName);

                    _repository.Update(std);
                }
            }
        }

        public IFormatProvider Culture { get; set; }
    }

}
